﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Photo.Core.Extensions
{
    /// <summary>
    /// 去除空格扩展方法
    /// </summary>
    public static class TrimExtension
    {
        /// <summary>
        /// 对象去除去除空格
        /// </summary>
        /// <param name="obj">对象</param>
        /// <returns></returns>
        public static object ToTrimString(this object obj)
        {
            try
            {
                Type type = obj.GetType();
                PropertyInfo[] props = type.GetProperties();

                Parallel.ForEach(props, p =>
                {
                    if (p.PropertyType.Name.Equals("String"))
                    {
                        var tmp = (string)p.GetValue(obj, null);
                        if (tmp != null)
                        {
                            p.SetValue(obj, tmp.Trim(), null);
                        }
                    }
                });

                return obj;
            }
            catch
            {
                return obj;
            }
        }

    }
}
